#!/usr/bin/env python3
"""
调试API错误的详细脚本
"""

import requests
import json

def debug_api():
    print("=== API调试 ===")
    
    # 测试配置
    config = {
        "grade_id": 1,
        "question_count": 5,
        "time_limit": 30
    }
    
    url = "http://127.0.0.1:8002/api/v1/math-test/generate-test"
    
    try:
        print(f"1. 发送请求到: {url}")
        print(f"   请求数据: {json.dumps(config, indent=2, ensure_ascii=False)}")
        
        response = requests.post(url, json=config, timeout=10)
        
        print(f"2. 响应状态码: {response.status_code}")
        
        if response.status_code == 200:
            result = response.json()
            print("✅ API调用成功!")
            print(f"   响应: {json.dumps(result, indent=2, ensure_ascii=False)}")
            return True
        else:
            print("API调用失败")
            print(f"   错误信息: {response.text}")
            
            # 尝试获取更详细的错误信息
            try:
                error_json = response.json()
                print(f"   错误详情: {json.dumps(error_json, indent=2, ensure_ascii=False)}")
            except:
                print("   无法解析错误详情")
            
            return False
            
    except requests.exceptions.ConnectionError:
        print("连接失败 - 服务器可能未运行")
        return False
        
    except requests.exceptions.Timeout:
        print("请求超时")
        return False
        
    except Exception as e:
        print(f"发生异常: {e}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == "__main__":
    debug_api()